package com.heukyosuk.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.heukyosuk.main.R;
import com.heukyosuk.main.R.layout;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class FileActivity extends Activity {
	public static final String SAVEPATH = Environment.getExternalStorageDirectory()+"/heukyosuk/";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ip);

		File dir = makeDirectory(SAVEPATH);					// �뤃�뜑 �깮�꽦
		String fileName = "myFile.txt";
		File file = makeFile(dir, (SAVEPATH + fileName));	// �뙆�씪 �깮�꽦

		Log.d("file", getAbsolutePath(dir));					// �젅�� 寃쎈줈
		Log.d("file", getAbsolutePath(file));

		String content = new String("媛��굹�떎�씪留덈컮�떎�궗�븘�옄李⑥뭅���뙆�븯");		// �뙆�씪 �벐湲�
		writeFile(file, content.getBytes());
		readFile(file);											// �뙆�씪 �씫湲�

	
		String[] list = getList(dir);							// �뵒�젆�넗由� �궡�슜 �뼸�뼱 �삤湲�
		for(String s : list){
			Log.d("file", s);
		}
	}

	// �뵒�젆�넗由� �깮�꽦 
	private File makeDirectory(String dirPath) {
		File dir = new File(dirPath);
		if (!dir.exists()) {
			dir.mkdirs();
			Log.d("file", "dir exists");
		} else {
			Log.d("file", "dir.existed");
		}
		return dir;
	}

	// �뙆�씪 �깮�꽦
	private File makeFile(File dir, String filePath) {
		File file = null;
		boolean isSuccess = false;
		
		if(dir.isDirectory()) {
			file = new File(filePath);
			if(file != null && !file.exists()){
				try {
					isSuccess = file.createNewFile();
					Log.d("file", "file exists");
				} catch (IOException e) {
					e.printStackTrace();
				} finally{
					Log.d("file", "�뙆�씪�깮�꽦 �뿬遺� = " + isSuccess);
				}
			} else {
				Log.d("file", "file existed");
			}
		}
		return file;
	}

	// (dir/file) �젅�� 寃쎈줈 �뼸�뼱�삤湲�
	private String getAbsolutePath(File file) {
		return "" + file.getAbsolutePath();
	}

	// (dir/file) �궘�젣 �븯湲�
	private boolean deleteFile(File file) {
		boolean result;
		if(file!=null && file.exists()) {
			file.delete();
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	// �뙆�씪�뿬遺� 泥댄겕 �븯湲�
	private boolean isFile(File file) {
		boolean result;
		if(file != null && file.exists() && file.isFile()) {
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	// �뵒�젆�넗由� �뿬遺� 泥댄겕 �븯湲�
	private boolean isDirectory(File dir) {
		boolean result;
		if(dir != null && dir.isDirectory()) {
			result = true;
		} else {
			result = false;
		}
		return result;
	}
	 
	// �뙆�씪 議댁옱 �뿬遺� �솗�씤 �븯湲�
	private boolean isFileExist(File file) {
		boolean result;
		if(file != null && file.exists()) {
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	// �뙆�씪 �씠由� 諛붽씀湲�
	private boolean reNameFile(File file , File new_name) {
		boolean result;
		if(file != null && file.exists() && file.renameTo(new_name)) {
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	// �뵒�젆�넗由ъ뿉 �븞�뿉 �궡�슜�쓣 蹂댁뿬 以��떎.
	private String[] getList(File dir){
		if(dir!=null && dir.exists())
			return dir.list();
		return null;
	}

	// �뙆�씪�뿉 �궡�슜 �벐湲�
	private boolean writeFile(File file , byte[] file_content){
		boolean result;
		FileOutputStream fos;
		if(file!=null && file.exists() && file_content!=null){
			try {
				fos = new FileOutputStream(file);
				try {
					fos.write(file_content);
					fos.flush();
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	// �뙆�씪 �씫�뼱 �삤湲� 
	private void readFile(File file) {
		int readcount = 0;
		if(file != null && file.exists()) {
			try {
				FileInputStream fis = new FileInputStream(file);
				readcount = (int) file.length();
				byte[] buffer = new byte[readcount];
				fis.read(buffer);
				for(int i = 0 ; i < file.length(); i++) {
					Log.d("file", ""+buffer[i]);
				}
				fis.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// �뙆�씪 蹂듭궗
	private boolean copyFile(File file , String save_file) {
		boolean result;
		if(file != null && file.exists()) {
			try {
				FileInputStream fis = new FileInputStream(file);
				FileOutputStream newfos = new FileOutputStream(save_file);
				int readcount = 0;
				byte[] buffer = new byte[1024];
				while((readcount = fis.read(buffer,0,1024))!= -1){
					newfos.write(buffer,0,readcount);
				}
				newfos.close();
				fis.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			result = true;
		} else {
			result = false;
		}
		return result;
	}
}